package com.gsit.job.services;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Component;

import com.gsit.job.domain.Person;

@Component
public class UrlParser {

	private static String [] emailPost = new String[]{"gmail.com","gmail.co.in","yahoo.com","yahoo.co.in","yahoo.in","rediffmail.com","rediff.com","mail.ru"};
	
	public Person parseData(WebDriver driver ){
		Person person = new Person();
		List<WebElement> elements = driver.findElements(By.className("dynamic_form_value"));
		Boolean firstElement = true;
		int pos = 1;
		for (WebElement webElement : elements) {
			if(webElement.getText()==null || webElement.getText().trim()==""){
				continue;
			}
			//System.out.println("webElement >>> "+webElement.getText());
			
			
			switch (pos) {
			case 1:
				person.setName(webElement.getText());
				break;
			case 4:
				if (isEmailPresent(webElement.getText())) {
					person.setEmail(getEmail(webElement.getText()));
				}
				
				break;
			case 5:
				if(UrlParser.isNumeric(webElement.getText())){
					person.setPhoneNumber(webElement.getText());
				}				
				break;			
			default:
				break;
			}
			pos++;
			/*if(pos == 1){
				if(isNamePresent(webElement.getText())){
					person.setName(webElement.getText());
					firstElement = false;
				}
			}else if(UrlParser.isEmailPresent(webElement.getText())){
				String preEmail = UrlParser.getPreEmail(webElement.getText());
				String postEmail = UrlParser.getPostEmail(webElement.getText());
				String email = preEmail+"@"+postEmail;
				person.setEmail(email);
			}else if(UrlParser.isNumeric(webElement.getText())){
				person.setPhoneNumber(webElement.getText());
			}*/
			
		}
		return person;
	}
	

	
	private static String getEmail(String value){
		
		Boolean result = Boolean.FALSE;
		String email = "";
		for (String emailP : emailPost) {
			if(value.contains(emailP)){
				result= true;
				email=findCorrectEmail(value, emailP);
				break;
			}
		}
		if(!result ){
			email = value;
		}
		return email;
	}
	
	private static String findCorrectEmail(String value, String email){
		value = value.substring(0,value.indexOf(email));
		if(value.contains(" ")){
			value= value.substring(value.lastIndexOf(" "));
		}
		value=value+"@"+email;
		return value;
	}
	
	public static void main(String[] args) {
		
		/*String  email= "wapnalimehetre<img src='http://www.spanjobs.com/include/themes/jobs/images/at.gif/' ALT='(at)' pagespeed_lsc_url='http://www.spanjobs.com/include/themes/jobs/images/at.gif'>gmail.com";
		UrlParser parser = new UrlParser();
		if(parser.isEmailPresent(email)){
			System.out.println(parser.getPreEmail(email));
			System.out.println(parser.getPostEmail(email));
		}*/
		
		String  email = "+9880395733";
		System.out.println(UrlParser.isNumeric(email));
		
	}
	
	private static Boolean isEmailPresent(String value){
		if(value.length() >30) return false;
		return !value.toLowerCase().contains("hidden");
	}
	
	private static Boolean isNamePresent(String value){
		Boolean result= Boolean.TRUE;
		if(value.contains("-")){
			result=Boolean.FALSE;
		}
		return result;
	}
	
	private static String getPostEmail(String value){
		return value.substring(value.indexOf(">")+1);
	}
	
	private static String getPreEmail(String value){
		return value.substring(0, value.indexOf("<img"));
	}
	
	private static boolean isNumeric(String value){
		try {
			if(value.length() >20) return false;
			if(value.toLowerCase().contains("hidden")){
				return false;
			}
			if(value.contains("+")){
				value = value.replace("+", "");
			}
			return value.matches("-?\\d+");  //match a number with optional '-' and decimal.
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	

}
